<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Properties in C#</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="C#, learn C#, properties, tutorial, programming language, .NET, Mono">
<meta name="description" content="This part of the C# tutorial covers C# properties.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>Properties</h1>

<p>
In this part of the C# tutorial, we will talk about properties.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script>

<p>
Properties are special kind of class members. We use predefined set and
get methods to access and modify them. Property reads and writes
are translated to get and set method calls. Accessing variables with 
a field notation (e.g. object.Name) is easier than with custom method
calls.(e.g. object.GetName()). However with properties, we still have
the advantage of encapsulation and information hiding. In other words,
properties shield the data from the outside world while having a 
convenient field access.
</p>

<pre class="code">
using System;


public class Person 
{
    private string _name; 

    public string Name
    {
        get { return _name; }
        set { _name = value;} 
    }
}

public class CSharpApp
{
    static void Main()
    {
        Person p = new Person();
        p.Name = "Jane";
        
        Console.WriteLine(p.Name);
    }
}
</pre>

<p>
We have a simple Person class with one property.
</p>

<pre class="explanation">
public string Name
{
   ...
}
</pre>

<p>
We have a property that is called Name. It looks
like a regular method declaration. The difference is
that it has specific accessors called <code>get</code> 
and <code>set</code>.
</p>

<pre class="explanation">
get { return _name; }
set { _name = value; } 
</pre>

<p>
A <code>get</code> property accessor is used to return the 
property value, and a <code>set</code> accessor is used to 
assign a new value. The <code>value</code> keyword is used 
to define the value being assigned by the set indexer.
</p>

<pre class="explanation">
Person p = new Person();
p.Name = "Jane";

Console.WriteLine(p.Name);
</pre>

<p>
We create an instance of the Person class. We access
the member field using the field notation.
</p>

<pre>
$ ./properties.exe 
Jane
</pre>

<p>
This is the outcome of the program. 
</p>

<h2>Read-only properties</h2>

<p>
It is possible to create read-only properties. To create a read-only
property, we omit the set accessor and provide only the get accessor
in the implementation.
</p>


<pre class="code">
using System;


public class Person 
{
    private string _name = "Jane"; 

    public string Name
    {
        get { return _name; }
    }
}

public class CSharpApp
{
    static void Main()
    {
        Person p = new Person();
        // p.Name = "Jane";
        
        Console.WriteLine(p.Name);
    }
}
</pre>

<p>
In the preceding example, we demonstrate the use of
a read-only property. 
</p>

<pre class="explanation">
private string _name = "Jane";
</pre>

<p>
We initialize the member right away, because later
it is not possible. 
</p>


<pre class="explanation">
public string Name
{
    get { return _name; }
}
</pre>

<p>
We make the property read-only by providing a get accessor only.
</p>

<pre class="explanation">
// p.Name = "Jane";
</pre>

<p>
This line is now commmented. We cannot change the property. If
we uncommented the line, the Mono C# compiler would issue the
following error: <code>readonly.cs(19,11): error CS0200: Property or 
indexer `Person.Name' cannot be assigned to (it is read-only)</code>.
</p>

<h2>Automatic properties</h2>

<p>
C# version 3.0 introduced auto-implemented or automatic properties.
In a software project, there are lots of simple properties, that only
set or get some simple values. To simplify programmming and to make
the code shorter, automatic properties were created. Note that we
cannot use automatic properties in all cases. Only for the simple
ones. 
</p>

<pre class="code">
using System;


public class Person 
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class CSharpApp
{
    public static void Main()
    {
        Person p = new Person();
        p.Name = "Jane";
        p.Age = 17;
        
        Console.WriteLine("{0} is {1} years old", 
            p.Name, p.Age);
    }
}
</pre>

<p>
This code is much shorter. We have a person class in which we
have two properties.
</p>

<pre class="explanation">
public string Name { get; set; }
public int Age { get; set; }
</pre>

<p>
Here we have two automatic properties. There is no implementation
of the accessors. And there are no member fields. The compiler will
do the rest for us. 
</p>

<pre class="explanation">
Person p = new Person();
p.Name = "Jane";
p.Age = 17;

Console.WriteLine("{0} is {1} years old", 
    p.Name, p.Age);
</pre>

<p>
We normally use the properties as usual. 
</p>

<pre>
$ ./automatic.exe 
Jane is 17 years old
</pre>

<p>
Output of the example. 
</p>

<h2>Other notes</h2>

<p>
We can mark properties with access modifiers like <code>public</code>, 
<code>private</code> or <code>protected</code>. Properties can be 
<code>static</code> also. C# supports also <code>abstract</code>, 
<code>virtual</code> and <code>sealed</code> properties. They are
used similarly like for regular methods. 
</p>


<pre class="code">
using System;


public class Base 
{
    protected string _name = "Base class"; 

    public virtual string Name
    {
        set { _name = value; }
        get { return _name; }
    }
}

public class Derived : Base
{
    protected new string _name = "Derived class"; 

    public override string Name
    {
        set { _name = value; }
        get { return _name; }
    }
}

public class CSharpApp
{
    public static void Main()
    {
        Base b = new Base();
        Derived d = new Derived();
        
        Console.WriteLine(b.Name);
        Console.WriteLine(d.Name);
    }
}
</pre>

<p>
In the preceding example, we define a virtual property and
override it in the Derived class. 
</p>

<pre class="explanation">
public virtual string Name
{
    set { _name = value; }
    get { return _name; }
}
</pre>

<p>
The Name property is marked with the <code>virtual</code> keyword.
</p>

<pre class="explanation">
protected new string _name = "Derived class"; 
</pre>

<p>
We are hiding a member in the Derived class. To supress the compiler
warning, we use the <code>new</code> keyword. 
</p>

<pre class="explanation">
public override string Name
{
    set { _name = value; }
    get { return _name; }
}
</pre>

<p>
And here we override the Name property of the Base class. 
</p>


<p>
This chapter covered C# properties. We described the properties and
showed, how they are implemented. We mentioned automatic properties 
and read-only properties.
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified October 31, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
